import axios from 'axios'
axios.defaults.withCredentials = true
const request = axios.create({
  baseURL: "http://localhost:8080",
  headers: {
    token:"Bearer "
  },

})

// 请求拦截器
request.interceptors.request.use((config) => {
  return config;
})

// 响应拦截器
request.interceptors.response.use(response => {
  return response;
})

export const get = async (url,headers,options) => {
  const response = await request({
    url,
    method:"GET",
    headers,
    ...options
  })

  let r = authReuqest(response)
  return r;
}
export const post = async (url, data, headers, options) => {
  const response = await  request({
    url,
    method:"POST",
    data: data,
    headers,
    ...options
  })
  let r = authReuqest(response)
  return r;
}

export const put = async (url, data, headers, options) => {
  const response = await  request({
    url,
    method:"PUT",
    data: data,
    headers,
    ...options
  })
  let r = authReuqest(response)
  return r;
}

function authReuqest (response) {
  let r = response.data;
  if (r.code === 200) {
    return r.data;
  }
  // 失败的
  return Promise.reject(r);
}

export default request;